var $editDialog;
var getEditDialog;
//首页跳转
var itemSearch;
require(['grid', 'topBar', 'layout'], function () {
    var $top = $('#top');
    var postData = {};
    $top.topBar({
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '手动修复',
                type: 'btn',
                authority: '',
                click: function () {
                    var selectRowIds = $('#grid').getGridParam().selarrrow;
                    var carIds = [];
                    $.each(selectRowIds, function (index, item) {
                        var row = $('#grid').getRowData(item);
                        var resolveStr = row.resolve;
                        var resolve = resolveStr.substr(resolveStr.indexOf('>') + 1, resolveStr.indexOf('</') - resolveStr.indexOf('>') - 1);
                        if (resolve > 0) {
                            carIds.push(row.carId);
                        }
                    });
                    if (carIds.length == 0) {
                        $.messager.alert("提示", "请选择要修复的车辆", "info");
                        return;
                    }
                    $.messager.confirm("是否修复", "确定手工修复选择的车辆？", function (r) {
                        if (r) {
                            $.ajaxExt({
                                url: "/reportExaminReport/manualRepirFault.htm",
                                data: {orgIds: carIds.join(',')}
                            }).done(function (data) {
                                $('#grid').trigger("reloadGrid");
                            });
                        }
                    });
                }
            },
            {
                text: '查询',
                type: 'btn',
                click: function () {
                    if (getPostData()) {
                        $('#grid').grid('setGridParam', {
                            page: 1,
                            datatype: "json",
                            postData: postData
                        }).trigger("reloadGrid");
                    }
                    else {
                        $.messager.alert("提示", "请选择查询车辆/机构。", "info");
                    }
                }
            }
        ]
    });

    $('body').layout();

    getEditDialog = function (data) {
        if (!$editDialog) {
            $editDialog = $('<div>').dialog({
                url: './mainte/report/reportExaminReportDetails.htm',
                height: 480,
                width: 820,
                title: '故障明细'
            });
        }
        $editDialog.dialog('open');
        var postData = {
            carId: data.carId
        }
        $editDialog.dialog('exec', 'searchData', postData);
    }

    $('#grid').grid({
        multiselect: true,
        url: "/reportExaminReport/searchLastExaminReport.htm",
        colModel: [
            {name: "carId", index: "carId", label: 'carId', hidden: true},
            {name: "licPltNo", label: '车牌号', width: '13%'},
            {name: "lineName", label: '运营线路', width: '13%'},
            {name: "examTime", label: '上次体检时间', width: '13%'},
            {
                name: "mftTotal", label: '故障总数', width: '13%', align: "center",
                formatter: function (rowIndex, val, rowData) {
                    return '<span class="num">{0}</span>'.format(rowData.mftTotal);
                }
            },
            {
                name: "resolve", index: "resolve", label: '未解决', width: '13%', align: "center",
                formatter: function (rowIndex, val, rowData) {
                    if (rowData.resolve > 0) {
                        return '<span class="no-solute">{0}</span>'.format(rowData.resolve);
                    }
                    else {
                        return '<span class="solute">{0}</span>'.format(rowData.resolve);
                    }
                }
            },
            {
                name: "operate", width: '13%', label: '操作', title: false, align: "center",
                formatter: 'operArea',
                formatoptions: [{
                    text: '查看',
                    css: 'operate-scan',
                    click: function (data) {
                        getEditDialog(data);
                    }
                }]
            }
        ]
    });

    function getPostData() {
        postData = {};
        var flag = false;
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.CAR) {//是否包含carIds
            postData.type = "CAR";
            postData.orgIds = nodes.CAR;
            flag = true;
        }
        if (nodes.LINE) {//是否包含lineIds
            postData.type = "LINE";
            postData.orgIds = nodes.LINE;
            flag = true;
        }
        return flag;
    }

    //导出
    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") > 0) {
            if (getPostData()) {
                var url = "/reportExaminReport/exportLastExaminReport" + suffix + ".htm";
                $.exportFile(url, postData);
            }
            else {
                $.messager.alert("提示", "请选择查询车辆/机构。", "info");
            }
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }
    
    itemSearch = function (carId) {
        //切换成车
        commonFun.showLeftTree(commonFun.carTree);
        //勾选并展开节点
        commonFun.setCheckedNodes(carId);
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    };
});